#!/bin/sh /etc/rc.common
START=99
STOP=10

enable=$(uci get easymesh.config.enabled 2>/dev/null)
mesh_bat0=$(uci get network.bat0 2>/dev/null)
mesh_nwi_mesh0=$(uci get network.nwi_mesh0 2>/dev/null)
mesh_mesh0=$(uci get wireless.mesh0 2>/dev/null)
lan=$(uci get network.lan.ifname 2>/dev/null)
mesh_id=$(uci get easymesh.config.mesh_id 2>/dev/null)
apRadio=$(uci get easymesh.config.apRadio 2>/dev/null)
mesh0_apRadio=$(uci get wireless.mesh0.device 2>/dev/null)
encryption=$(uci get easymesh.config.encryption 2>/dev/null)
key=$(uci get easymesh.config.key 2>/dev/null)

start(){	
	if [ "$enable" == 1 ]; then
		if [ "$mesh_bat0" != "interface" ]; then
			uci set network.bat0=interface
			uci set network.bat0.proto='batadv'
			uci set network.bat0.routing_algo='BATMAN_IV'
			uci set network.bat0.aggregated_ogms='1'
			#uci set network.bat0.gw_bandwidth='10000/2000'
			uci set network.bat0.ap_isolation='0'
			uci set network.bat0.bonding='0'
			uci set network.bat0.fragmentation='1'
			uci set network.bat0.gw_mode='off'
			#uci set network.bat0.gw_sel_class='20'
			uci set network.bat0.log_level='0'
			uci set network.bat0.orig_interval='1000'
			uci set network.bat0.bridge_loop_avoidance='1'
			uci set network.bat0.distributed_arp_table='1'
			uci set network.bat0.multicast_mode='1'
			uci set network.bat0.network_coding='0'
			uci set network.bat0.hop_penalty='30'
			uci set network.bat0.isolation_mark='0x00000000/0x00000000'

			uci set network.lan.ifname="${lan} bat0"
			uci commit network
		fi

		if [ "$mesh_nwi_mesh0" != "interface" ]; then
			uci set network.nwi_mesh0=interface
			uci set network.nwi_mesh0.proto='batadv_hardif'
			uci set network.nwi_mesh0.mtu='2304'
			uci set network.nwi_mesh0.master='bat0'
			uci commit network
		fi

		if [ "$mesh_mesh0" != "wifi-iface" ]; then
			uci set wireless.mesh0=wifi-iface
			uci set wireless.mesh0.device=$apRadio
			uci set wireless.mesh0.ifname='mesh0'
			uci set wireless.mesh0.network='nwi_mesh0'
			uci set wireless.mesh0.mode='mesh'
			uci set wireless.mesh0.mesh_fwding='0'
			uci set wireless.mesh0.mesh_id=$mesh_id
			uci commit wireless
		fi

		if [ "$mesh_mesh0" = "wifi-iface" ]; then
			if [ "$mesh0_apRadio" != "$apRadio" ]; then
				uci set wireless.mesh0.device=$apRadio
				uci commit wireless
			fi
		fi

		if [ "$encryption" != 1 ]; then
			uci set wireless.mesh0.encryption='none'
			uci commit wireless
		else
			uci set wireless.mesh0.encryption='sae'
			uci set wireless.mesh0.key=$key
			uci commit wireless
		fi

		/etc/init.d/network restart
	else
		stop
	fi
}

stop(){
	if [ "$enable" != 1 ]; then
		if [ "$mesh_bat0" = "interface" ]; then
			uci delete network.bat0
			uci commit network
			sed -i 's/ bat0//' /etc/config/network
		fi

		if [ "$mesh_nwi_mesh0" = "interface" ]; then
			uci delete network.nwi_mesh0
			uci commit network
		fi

		if [ "$mesh_mesh0" = "wifi-iface" ]; then
			uci delete wireless.mesh0
			uci commit wireless
		fi

		/etc/init.d/network restart
	fi
}

restart() {
	start
}
